<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Upgrade from preem to royal</title>
<link rel="stylesheet" type="text/css" href="{*__CURRENT__}/../base.css" />
<script type="text/javascript" src="{*__CURRENT__}/../mootools.js"></script>
</head>
<body>
<div id="output">
</div>
<script type="text/javascript">

var output;

function append(msg,isfail){
	var color = (isfail) ? 'red' : 'black';
	var p = new Element('p');
	p.set('html',msg);
	p.setStyle('color',color); 
	output.grab(p);
}

var updateOk = false; 
var page = 0;

function updatePost(){
	if(updateOk){
		return;
	}
    new Request({
        url:'__URL__/upgradePost',
        method:'get',
        onSuccess:function(txt){
           var json = JSON.decode(txt);
           if(json.status){
               if(json.info == 'ok' ){
            	   updateOk = true;
            	   append('all posts have been migrated');
               }else{
                   page++;
	               append('migrate post【part:'+page+'】 ok');
	               //更新post
	               updatePost();
               }
           }else{
               append('migrate post fail',true);
           }
        },
        onRequest:function(){
            append('migrate post...');
        }
    }).send("page="+page);

}

$(window).addEvent('domready',function(){
	output = $('output');
	append('migrate start');
	//更新category
	new Request({
	    url:'__URL__/upgradeCategory',
        method:'get',
	    onSuccess:function(txt){
		   var json = JSON.decode(txt);
		   if(json.status){
			   append('migrate category ok');
			   //更新post
			   updatePost();
		   }else{
               append('migrate category fail',true);
		   }
		},
		onRequest:function(){
			append('migrate category...');
		}
	}).send();
	
});


</script>
</body>
</html>